import {pAjax} from "./gather";
//获取本地地址

let id = window.localStorage.getItem('id')
let token = window.localStorage.getItem('token')

const off = document.querySelector('.off');
const on = document.querySelector('.on');
const name1 = document.querySelector('.nickname');
fun();
async function  fun(){
    //验证
    if(!id || !token){
        off.classList.add('active');
        on.classList.remove('active');
        return
    }

    let res = await pAjax({
        url:'http://localhost:8888/users/info',
        data:{id},
        headers: { authorization: token },
        dataType:'json'
    })
    console.log(res)


    off.classList.remove('active');
    on.classList.add('active');
    
    if(res.code !==1){
        off.classList.add('active');
        on.classList.remove('active');
        return
    }
    name1.innerHTML = res.info.nickname;

    const logout = document.querySelector('.logout');
    logout.addEventListener('click',async function(){
        let res = await pAjax({
            url:'http://localhost:8888/users/logout',
            data:{id},
            dataType:'json'
        })
        console.log(res)
        if(res.code ==1){
            on.classList.remove('active');
            off.classList.add('active');
            return
        }
    })
}

